<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceGetFriendList extends core {

    public function process() {
        $limit = 20;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit;
        $total = 0;

        $data = $this->db->get_results("SELECT * FROM guests WHERE user_id={$user_id} AND is_invite = 0 ORDER BY firstname asc LIMIT $from,$limit;");
        $total = $this->db->get_row("SELECT count(guest_id) as total FROM guests WHERE user_id={$user_id} AND is_invite = 0;");

        if (ceil($total->total / $limit) <= $page) {
            $end_paging = 1;
        } else {
            $end_paging = 0;
        }

        $html = '';
        foreach ($data as $guest) {
            $html.='<div class="wd-add-guest-list">
                            <input type="checkbox" id="wd-checkbox-' . $guest->guest_id . '" rel="' . $guest->guest_id . '" name="fiance_condition" class="regular-checkbox-addguest checkbox-invite-friend">
                            <label for="wd-checkbox-' . $guest->guest_id . '"></label>
                            <div class="wd-ava-addguest-list">';
            if ($guest->photo != "") {
                $html.='<img src="'.$guest->photo.'" class="wd-addfbguest-ava"/>';
            } else {
                $html.='<img src="img/no-ava.png" class="wd-addfbguest-ava"/>';
            }

            $html.='<span class="wd-addfbguest-name">' . $guest->firstname . ' ' . $guest->lastname . '</span>
                            </div>
                        </div>';
        }
        if ($html == '') {
            $html = '<div style="margin: 4px 12px 0 20px;color: #876d63;">No guest was found.</div>';
            $this->_JSONResponse(array('error' => 1, 'html' => $html));
        }
        $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
    }

}

$processor = new webServiceGetFriendList();
$processor->process();
?>